/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cfs.controller;

import cfs.databean.CFSTransaction;
import cfs.databean.Customer;
import cfs.databean.FundPriceHistory;
import cfs.databean.Position;
import cfs.helper.SendMail;
import cfs.model.FundPriceHistoryDAO;
import cfs.model.Model;
import cfs.model.PositionDAO;
import cfs.model.TransactionDAO;

/**
 * 
 * @author danny seow
 */
public class EmailService {
	private PositionDAO positionDAO;
	private TransactionDAO transDAO;
	private FundPriceHistoryDAO fundPriceDAO;

	public EmailService(Model model) {
		positionDAO = model.getPositionDao();
		transDAO = model.getTransactionDao();
		fundPriceDAO = model.getFundHistoryDao();
	}

	public void activeEmailService(int type, Customer customer) {
		String from = "c.finance.services@gmail.com";
		String to = customer.getEmail();
		String subject = "";
		String message = "";
		if (type != 1 && type != 2 && type != 3) {
			return;
		}
		switch (type) {
		// Using reset password
		case 1:
			subject = "Carnegie Finances Password Reset";
			message = generateMsgEmailReset(customer);
			break;
		// Using for new customer
		case 2:
			subject = "Carnegie Finances Welcomes You";
			message = generateMsgEmailNewCustomer(customer);
			break;
		// Using for daily stats;
		case 3:
			subject = "Carnegie Finances Update";
			message = generateMsgEmailDailyUpdate(customer);
			break;
		}
		System.out.println("Message: " + message);
		if (message.equalsIgnoreCase("")) {
			return;
		}
		SendMail sendMail = new SendMail(from, to, subject, message);
		sendMail.send();
	}

	private String generateMsgEmailReset(Customer customer) {
		StringBuilder s = new StringBuilder();
		s.append("Carnegie Finances Services is your number one choice. \n")
				.append("\n")
				.append("Good Morning " + customer.getFirstname() + "\n")
				.append("Your account has been reset: \n")
				.append("Username: " + customer.getUsername() + "\n")
				.append("Password: " + customer.getPassword() + "\n");
		String finalResult = s.toString();
		return finalResult;
	}

	private String generateMsgEmailNewCustomer(Customer customer) {
		StringBuilder s = new StringBuilder();
		s.append("Carnegie Finances Services is your number one choice. \n")
				.append("\n")
				.append("Good Morning " + customer.getFirstname()
						+ " This is your new account\n")
				.append("Your account has details as follows: \n")
				.append("Username: " + customer.getUsername() + "\n")
				.append("Password: " + customer.getPassword() + "\n");
		String finalResult = s.toString();
		return finalResult;
	}

	private String generateMsgEmailDailyUpdate(Customer customer) {
		StringBuilder s = new StringBuilder();

		try {
			CFSTransaction[] cfList = transDAO.getTransactions(customer
					.getCustomer_id());
			if (cfList != null && cfList.length < 0) {
				return "";
			}
			s.append("Carnegie Finances Services is your number one choice. \n")
					.append("\n")
					.append("Good Morning " + customer.getFirstname() + "\n\n")
					.append("The following is updated transaction: \n");
			for (int z = 0; z < cfList.length; z++) {
				if (cfList[z].getTransaction_type() == 1) {
					s.append(z + 1 + ". Deposit : $" + cfList[z].getAmount()
							* 0.01 + " \n\n");
				} else if (cfList[z].getTransaction_type() == 2) {
					s.append(z + 1 + ". Withdrawn : $" + cfList[z].getAmount()
							* 0.01 + " \n\n");
				} else if (cfList[z].getTransaction_type() == 3
						|| cfList[z].getTransaction_type() == 4) {
					if (cfList[z].getFund_id() != 0) {
						FundPriceHistory latestFundByType = fundPriceDAO
								.getLatestFundByType(cfList[z].getFund_id());
						Position[] position = positionDAO.getPosition(
								cfList[z].getFund_id(),
								customer.getCustomer_id());
						double shareValue = (double) (position[0].getShares() * 0.001);
						double fundPrice = (double) (latestFundByType
								.getPrice() * 0.01);
						s.append(z + 1 + ". Fund: " + cfList[z].getFund_id()
								+ " \n");
						s.append("Market closing price: $" + fundPrice + "\n");
						s.append("Your share position: " + shareValue + "\n\n");
					}
				}
			}
			s.append("\n\n\n\n");
			s.append("------------------------------------- \n");
			s.append("Total cash: $" + (double) (customer.getCash() * 0.01));
			String finalResult = s.toString();
			return finalResult;
		} catch (Exception ex) {
			return "";
		}
	}
}
